﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DataManagerTests.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the <seealso cref="DataManagerTests" /> type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Business.Tests
{
    using System;
    using System.IO;

    using Hisaab.Business.Interfaces;
    using Hisaab.Common.Wrappers;

    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Moq;

    /// <summary>
    /// Defines the <seealso cref="DataManagerTests"/> type.
    /// </summary>
    [TestClass]
    public class DataManagerTests
    {
        /// <summary>
        /// Member variable _mockAccountManager
        /// </summary>
        private Mock<IAccountManager> _mockAccountManager;

        /// <summary>
        /// Member variable _mockTransactionManager
        /// </summary>
        private Mock<ITransactionManager> _mockTransactionManager;

        /// <summary>
        /// Member variable _mockTagManager
        /// </summary>
        private Mock<ITagManager> _mockTagManager;

        /// <summary>
        /// Member variable _mockFileWrapper
        /// </summary>
        private Mock<IFileWrapper> _mockFileWrapper;

        /// <summary>
        /// Initializes this instance.
        /// </summary>
        [TestInitialize]
        public void Initialize()
        {
            _mockAccountManager = new Mock<IAccountManager>();
            _mockTransactionManager = new Mock<ITransactionManager>();
            _mockTagManager = new Mock<ITagManager>();
            _mockFileWrapper = new Mock<IFileWrapper>();
        }

        /// <summary>
        /// Constructors the should throw exception if account manager is null.
        /// </summary>
        [TestMethod, ExpectedException(typeof(ArgumentNullException))]
        public void ConstructorShouldThrowExceptionIfAccountManagerIsNull()
        {
            new DataManager(null, _mockTransactionManager.Object, _mockTagManager.Object, _mockFileWrapper.Object);
        }

        /// <summary>
        /// Constructors the should throw exception if transaction manager is null.
        /// </summary>
        [TestMethod, ExpectedException(typeof(ArgumentNullException))]
        public void ConstructorShouldThrowExceptionIfTransactionManagerIsNull()
        {
            new DataManager(_mockAccountManager.Object, null, _mockTagManager.Object, _mockFileWrapper.Object);
        }

        /// <summary>
        /// Constructors the should throw exception if tag manager is null.
        /// </summary>
        [TestMethod, ExpectedException(typeof(ArgumentNullException))]
        public void ConstructorShouldThrowExceptionIfTagManagerIsNull()
        {
            new DataManager(_mockAccountManager.Object, _mockTransactionManager.Object, null, _mockFileWrapper.Object);
        }

        /// <summary>
        /// Constructors the should throw exception if file wrapper is null.
        /// </summary>
        [TestMethod, ExpectedException(typeof(ArgumentNullException))]
        public void ConstructorShouldThrowExceptionIfFileWrapperIsNull()
        {
            new DataManager(_mockAccountManager.Object, _mockTransactionManager.Object, _mockTagManager.Object, null);
        }

        /// <summary>
        /// Exports the should throw exception if file name is null.
        /// </summary>
        [TestMethod, ExpectedException(typeof(ArgumentNullException))]
        public void ExportShouldThrowExceptionIfFileNameIsNull()
        {
            var dm = new DataManager(_mockAccountManager.Object, _mockTransactionManager.Object, _mockTagManager.Object, _mockFileWrapper.Object);
            dm.ExportData(null, 1, "stlkhdg");
        }

        /// <summary>
        /// Exports the should throw exception if file name is empty.
        /// </summary>
        [TestMethod, ExpectedException(typeof(ArgumentNullException))]
        public void ExportShouldThrowExceptionIfFileNameIsEmpty()
        {
            var dm = new DataManager(_mockAccountManager.Object, _mockTransactionManager.Object, _mockTagManager.Object, _mockFileWrapper.Object);

            dm.ExportData(string.Empty, 1, "stlkhdg");
        }

        /// <summary>
        /// Writes the export file should delete file if already exists.
        /// </summary>
        [TestMethod]
        public void WriteExportFileShouldDeleteFileIfAlreadyExists()
        {
            var dm = new DataManager(_mockAccountManager.Object, _mockTransactionManager.Object, _mockTagManager.Object, _mockFileWrapper.Object);
            _mockFileWrapper.Setup(x => x.Exists("file.ext")).Returns(true);
            _mockFileWrapper.Setup(x => x.OpenWrite("file.ext")).Returns(new FileStream(Path.GetTempFileName(), FileMode.OpenOrCreate));

            dm.WriteExportFile("file.ext", new DataManager.BackupData(), "stlkhdg");
            _mockFileWrapper.Verify(x => x.Delete("file.ext"), Times.Once());
        }
    }
}